"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.checkCookieTicket = checkCookieTicket;
exports.checkTicket = checkTicket;
exports.checkUrlTicket = checkUrlTicket;
exports.default = _default;
exports.getCookieTicket = getCookieTicket;
exports.getUrlTicket = getUrlTicket;

var _config = require("./config");

var _casCookie = require("@yjtec/cas-cookie");

var _utils = require("./utils/utils");

var _service = _interopRequireDefault(require("./service"));

var _events = _interopRequireDefault(require("./events"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _default() {
  return _ref.apply(this, arguments);
}

function _ref() {
  _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var re, _re;

    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return checkCookieTicket();

          case 2:
            re = _context.sent;

            if (!re) {
              _context.next = 7;
              break;
            }

            return _context.abrupt("return", re);

          case 7:
            _context.next = 9;
            return checkUrlTicket();

          case 9:
            _re = _context.sent;

            if (!_re) {
              _context.next = 14;
              break;
            }

            return _context.abrupt("return", _re);

          case 14:
            _events.default.emit('cas.nologin');

            return _context.abrupt("return", Promise.resolve(false));

          case 16:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _ref.apply(this, arguments);
}

function checkCookieTicket() {
  return _checkCookieTicket.apply(this, arguments);
}

function _checkCookieTicket() {
  _checkCookieTicket = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
    var cookieTicket, re;
    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            alert(1212);
            cookieTicket = getCookieTicket();

            if (!cookieTicket) {
              _context2.next = 15;
              break;
            }

            _context2.next = 5;
            return checkTicket(cookieTicket);

          case 5:
            re = _context2.sent;

            if (!re) {
              _context2.next = 11;
              break;
            }

            //ticket合法
            _events.default.emit('cas.check.success', {
              ticket: cookieTicket,
              user: re
            });

            return _context2.abrupt("return", Promise.resolve({
              ticket: cookieTicket,
              user: re
            }));

          case 11:
            //tiket不合法
            _events.default.emit('cas.check.error', {
              ticket: cookieTicket
            });

            return _context2.abrupt("return", Promise.resolve(false));

          case 13:
            _context2.next = 16;
            break;

          case 15:
            return _context2.abrupt("return", Promise.resolve(false));

          case 16:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _checkCookieTicket.apply(this, arguments);
}

function checkUrlTicket() {
  return _checkUrlTicket.apply(this, arguments);
}

function _checkUrlTicket() {
  _checkUrlTicket = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
    var urlTicket, re;
    return regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            urlTicket = getUrlTicket();

            if (!urlTicket) {
              _context3.next = 14;
              break;
            }

            _context3.next = 4;
            return checkTicket(urlTicket);

          case 4:
            re = _context3.sent;

            if (!re) {
              _context3.next = 10;
              break;
            }

            //ticket合法
            _events.default.emit('cas.login.success', {
              ticket: urlTicket,
              user: re
            });

            return _context3.abrupt("return", Promise.resolve({
              ticket: urlTicket,
              user: re
            }));

          case 10:
            //tiket不合法
            _events.default.emit('cas.login.error', {
              ticket: urlTicket
            });

            return _context3.abrupt("return", Promise.resolve(false));

          case 12:
            _context3.next = 15;
            break;

          case 14:
            return _context3.abrupt("return", Promise.resolve(false));

          case 15:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _checkUrlTicket.apply(this, arguments);
}

function checkTicket(_x) {
  return _checkTicket.apply(this, arguments);
}

function _checkTicket() {
  _checkTicket = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(ticket) {
    var uri, re;
    return regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            uri = _config.casConfig.get('action');
            _context4.next = 3;
            return (0, _service.default)(uri, ticket);

          case 3:
            re = _context4.sent;

            if (!(re.errcode == 0 && re.data)) {
              _context4.next = 8;
              break;
            }

            return _context4.abrupt("return", re.data);

          case 8:
            return _context4.abrupt("return", false);

          case 9:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _checkTicket.apply(this, arguments);
}

function getCookieTicket() {
  return _casCookie.cookie.getCookie();
}

function getUrlTicket() {
  return (0, _utils.getUrlQuery)(window.location.href, 'ticket');
}